<?php
/**
 *  IP 地理位置查询类 修改自http://dev.maxmind.com/geoip/install/country
 * @category   COM
 * @package  COM
 * @author    cjli
 */
class Ip
{
    /**
     * GeoIP.dat文件指针
     *
     * @var resource
     */
    private $gi;
    
    private $record;
    
    /**
     * 读取数据类型
     * @var int 0 or 1 0:只要国家,1:城市
     */
    public $type; 

    
    /**
     * 构造函数，打开 GeoIP.dat 文件并初始化类中的信息
     *
     * @param string $type 
     * @return IpLocation
     */
    public function __construct($ip)
    {
     	include(VENDOR_PATH."GeoIp/geoipcity.inc");
			include(VENDOR_PATH."GeoIp/geoipregionvars.php");
      $this->gi = geoip_open(VENDOR_PATH."GeoIp/GeoLiteCity.dat",GEOIP_STANDARD);
      $this->record = geoip_record_by_addr($this->gi,$ip);
    }
    
    public function country_code()
    {
    	return $this->record->country_code;
    }
    
    public function country_code3()
    {
    	return $this->record->country_code3;
    }
    
    public function country_name()
    {
    	return $this->record->country_name;
    }
    
    public function region_name()
    {
    	return $GEOIP_REGION_NAME[$this->record->country_code][$this->record->region];
    }
    
    public function city_name()
    {
    	return $this->record->city;
    }
    
    public function postal_code()
    {
    	return $this->record->postal_code;
    }
    
}